local bufferline = require("bufferline")
local utils = require("utils")

local hide = {
	qf = true,
}

bufferline.setup({
	options = {
		mode = "buffers",
		max_name_length = 30,
		sort_by = "none",
		offsets = {
			{
				filetype = "NvimTree",
				text = "File Explorer",
				highlight = "Directory",
				padding = 1,
			},
		},
		custom_filter = function(bufnr)
			return not hide[vim.bo[bufnr].filetype]
		end,
	},
})

for i = 1, 9 do
	vim.keymap.set("n", "<leader>" .. i, function()
		bufferline.go_to(i, true)
	end)
end

vim.keymap.set("n", "<leader>q", "<Cmd>BufferLineCyclePrev<CR>")
vim.keymap.set("n", "<leader>e", "<Cmd>BufferLineCycleNext<CR>")
vim.keymap.set("n", "<C-q>", "<Cmd>BufferLineMovePrev<CR>")
vim.keymap.set("n", "<C-e>", "<Cmd>BufferLineMoveNext<CR>")
vim.keymap.set("n", "ZZ", function()
	if vim.bo.modified then
		utils.log_err("No write since last change")
		return
	end
	vim.cmd.bdelete()
end)

vim.api.nvim_create_user_command("CL", "BufferLineCloseLeft", {})
vim.api.nvim_create_user_command("CR", "BufferLineCloseRight", {})
